package drds.common.db.xml_mapping.parameter_and_result_value_type;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Date;


public class DateTypeHandler extends BaseTypeHandler<Date> {

  @Override
  public void setNonNullParameter(PreparedStatement preparedStatement, int index, Date parameter, JdbcType jdbcType)
    throws SQLException {
    preparedStatement.setTimestamp(index, new Timestamp(parameter.getTime()));
  }

  @Override
  public Date getNullableResult(ResultSet resultSet, String columnName)
    throws SQLException {
    Timestamp sqlTimestamp = resultSet.getTimestamp(columnName);
    if (sqlTimestamp != null) {
      return new Date(sqlTimestamp.getTime());
    }
    return null;
  }

  @Override
  public Date getNullableResult(ResultSet resultSet, int columnIndex)
    throws SQLException {
    Timestamp sqlTimestamp = resultSet.getTimestamp(columnIndex);
    if (sqlTimestamp != null) {
      return new Date(sqlTimestamp.getTime());
    }
    return null;
  }

}
